package com.gs.netty.core.utils;

import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

public class PropertiesUtils {


    public static void propertiesToObject(String prefix,final Properties p, final Object object) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Class<?> sourceClass = object.getClass();
        // 获取属性
       c: for (Field field : sourceClass.getFields()) {
            Class<?> paramType = field.getType();

            String name = field.getName();
            String propertyName = prefix + "."+name;
            Object arg = null;
            // 根据属性获取值


            // 判断是否是基本类型
            if (paramType.isPrimitive()||String.class.equals(paramType)) {
                String propertyValue = p.getProperty(propertyName);
                if (StringUtils.isEmpty(propertyValue)){
                    continue c;
                }
                // 基本数据类型的处理
                String typeName = paramType.getSimpleName();
                if ("int".equals(typeName) || "Integer".equals(typeName)) {
                    arg = Integer.parseInt(propertyValue);
                } else if ("long".equals(typeName) || "Long".equals(typeName)) {
                    arg = Long.parseLong(propertyValue);
                } else if ("double".equals(typeName) || "Double".equals(typeName)) {
                    arg = Double.parseDouble(propertyValue);
                } else if ("boolean".equals(typeName) || "Boolean".equals(typeName)) {
                    arg = Boolean.parseBoolean(propertyValue);
                } else if ("float".equals(typeName) || "Float".equals(typeName)) {
                    arg = Float.parseFloat(propertyValue);
                } else if("byte".equals(typeName) || "Byte".equals(typeName)) {
                    arg = Byte.parseByte(propertyValue);
                }else if("short".equals(typeName) || "Short".equals(typeName)) {
                    arg = Short.parseShort(propertyValue);
                } else if("char".equals(typeName) || "Character".equals(typeName)) {
                    arg = propertyValue.charAt(0);
                }else if("String".equals(typeName) || "string".equals(typeName)) {
                    arg = propertyValue;
                }
                else {
                    continue;
                }
            }else if (Class.class.equals(paramType)) {

            }

            else {
                String methodName = "get"+field.getName().substring(0,1).toUpperCase()+field.getName().substring(1);
                Method method = sourceClass.getMethod(methodName);
                Object propertieObject = method.invoke(object);
                propertiesToObject(propertyName,p,propertieObject);
            }


            // 调用 setter 方法给属性赋值
            if (arg != null) {
               String methodName ="set"+name.substring(0,1).toUpperCase()+name.substring(1);
               Method method = sourceClass.getMethod(methodName,paramType);
                if (method != null) {
                    method.invoke(object, arg);
                }

            }
        }
    }

}
